package com.lei.controller;

import com.alibaba.fastjson.JSON;
import com.lei.entity.Menu;
import com.lei.entity.Staff;
import com.lei.service.MenuService;
import com.lei.service.impl.MenuServiceImpl;
import com.lei.util.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author Lei Chu
 * @Date 2020/10/16 20:56
 */
@WebServlet("/menuServlet/*")
public class MenuServlet extends BaseServlet{

    private MenuService menuService = new MenuServiceImpl();

    /**
     * 功能描述:根据登录人的rid查出所有的权限，用于生成主菜单
     * @param request Req
     * @param response Resp
     * @return java.lang.String
     */
    public String getAllMenuByRid(HttpServletRequest request, HttpServletResponse response){
        Staff nowStaff = (Staff) request.getSession().getAttribute("nowStaff");
        List<Menu> menus = menuService.getAllMenuByRid(nowStaff.getRoleId());
        request.setAttribute("menus",menus);
        return Constants.FORWARD + "/homePage.jsp";
    }

    /**
     * 功能描述:查询数据库获得所有的权限，用于生成分配权限表单的生成
     * @param request Req
     * @param response Resp
     * @return java.lang.String
     */
    public String getAllMenu(HttpServletRequest request,HttpServletResponse response){
        List<Menu> allMenu = menuService.getAllMenu();
        request.setAttribute("allMenu",allMenu);
        return Constants.FORWARD + "/role.jsp";
    }

    /**
     * 功能描述:根据rid查找所有的pid，返回给页面 List ，用于分配权限时的 checkbox 的勾选
     * @param request Req
     * @param response Resp
     * @return java.lang.String
     */
    public String getMenuByRid(HttpServletRequest request,HttpServletResponse response){
        String rid = request.getParameter("rid");
        //返回给前段的 list
        List<Integer> list = menuService.getMenuByRid(rid);
        return JSON.toJSONString(list);
    }

    public String updateRoleMenu(HttpServletRequest request,HttpServletResponse response){
        // 获得rid
        String rid = request.getParameter("modifyPowerRoleId");
        // 获得勾选的所有权限
        String[] mids = request.getParameterValues("resource");
        String result = menuService.updateRoleMenu(rid,mids);
        return result;
    }


}
